#include "Restaurant.h"
#include <iostream>


void inline displayMenu(){

	cout << endl
		 << "1.		show orders"		<< endl
		 << "2.		show customers"		<< endl
		 << "3.		add customer"		<< endl
		 << "4.		remove customer"	<< endl
		 << "5.		take order"			<< endl
		 << "6.		change order"		<< endl
		 << "7.		remove order"		<< endl
		 << "99.	QUIT"				<< endl
		 << ":";
	return;
}



void inline readOrder(Restaurant& rest,int& index,string& drink,string& first,string& main,string& last){
			cout << rest.getCustomers();
			
			cout << "enter customer index: ";
			string strIndex;
			getline(cin,strIndex);
			index = atoi(strIndex.c_str());
			if (!rest.checkCustomerExists(index)){
				drink = "";
				first = "";
				main = "";
				last = "";
				return;
			}
			cout << endl
				 << "enter \"clear\" to delete previous values," << endl << "use a blank line to skip the course (skip to leave blank when taking order)."<< endl << endl
				 << "enter drink: ";
			getline(cin,drink);

			cout << endl
				 << "enter first: ";
			getline(cin,first);

			cout << endl
				 << "enter main: ";
			getline(cin,main);

			cout << endl
				 << "enter last: ";
			getline(cin,last);
}


int main(int argc, char** argv){
	Restaurant rest;
	
	cout << "WELCOME\n";
	string input;
	int option;
	//these are used to take orders
	string drink;
	string first;
	string main;
	string last;
	////
	while (true){
		cout << "========\t========";
		displayMenu();
		getline(cin,input);
		option = 0;
		option = atoi(input.c_str());
				
		switch(option){
		case 1:	{		// show orders
			cout << rest.getOrders();
			break;
				}
		case 2:	{		// show customer
			cout << rest.getCustomers();
			break;
				}
		case 3:	{		// add customer 
			cout << endl << endl
				 << "enter customer name: ";
			string name;
			getline(cin,name);
			int ret = rest.addCustomer(name);
			if (ret == -1){
				cout << "ERROR! the program will abort now" << endl;
				return 0;
				// 
			}
			break;
				}
		case 4:	{		// remove customer
			cout << rest.getCustomers();
			cout << "enter customer index to remove: ";
			string strIndex;
			getline(cin,strIndex);
			int index = atoi(strIndex.c_str());
			cout << rest.removeCustomer(index);
			break;
				}
					
		//take order
		case 5:{
			int index;
			readOrder(rest,index,drink,first,main,last);
			string retStr = rest.takeOrder(index, drink, first, main, last);
			// check if takeOrder() failed
			if (retStr.compare("ERR") == 0){
				cout << "ERROR: the program will abort" << endl;
				return 0;
			}

			cout << retStr;
			break;
			   }

		// change order
		case 6: {
			int index;
			readOrder(rest,index,drink,first,main,last);
			string retStr = rest.changeOrder(index, drink, first, main, last);
			//check if changeOrder failed
			if (retStr.compare("ERR") == 0){
				cout << "ERROR: the program will abort" << endl;
				return 0;
			}

			cout << retStr;
			break;
			   }
		//remove order
		case 7: {
			cout << rest.getOrders();
			cout << "enter order index to remove: ";
			string strIndex;
			getline(cin,strIndex);
			int index = atoi(strIndex.c_str());
			cout << rest.removeOrder(index);
			break;
				}
		case 99:
			exit(0);
		default:
			cin.ignore();
			cin.clear();
			cout << "plesae enter a valid number" << endl;
			continue;
		}
	}
}


